/**
 * 
 */
package net.it2pro.communication;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.RosterPacket.ItemType;

/**
 * @author nttuyen
 *
 */
public class GoogleBot {
	private final Logger log = Logger.getLogger(GoogleBot.class);
	
	private final String username;
	private final String password;
	
	private final ConnectionConfiguration config;
	
	private Connection connection;
	
	public GoogleBot(String username, String password) {
		this.username = username;
		this.password = password;
		
		this.config = new ConnectionConfiguration("talk.google.com", 5222, "googlemail.com");
	}
	
	public void start() throws XMPPException {
		log.debug("Init connection");
		this.connection = new XMPPConnection(this.config);
		log.debug("Init connection completed");
		log.debug("Connect to server");
		this.connection.connect();
		log.debug("Connect to server completed, prepair to login");
		this.connection.login(this.username, this.password);
		log.debug("Login completed!");
		
		ChatManager chatManager = this.connection.getChatManager();
		
		Roster roster = this.connection.getRoster();
		Collection<RosterEntry> entries = roster.getEntries();
		for(RosterEntry entry : entries) {
			this.log.debug("Entry: " + entry);
			this.log.debug("Entry status: " + entry.getStatus());
			this.log.debug("Entry Name: " + entry.getName());
			this.log.debug("Entry User: " + entry.getUser());
			this.log.debug("EntryType: " + entry.getType());
			Presence p = roster.getPresence(entry.getUser());
			this.log.debug("Entry STT: " + p.getStatus());
			this.log.debug("Entry Mode: " + p.getMode());
			this.log.debug("Entry Type: " + p.getType());
		}
		
		Presence presence = roster.getPresence("nttuyen87@gmail.com");
		log.debug("Presence: " + presence);
		log.debug("STT: " + presence.getStatus());
		
		Chat chat = chatManager.createChat("nttuyen87@gmail.com", new MessageListener() {
			public void processMessage(Chat from, Message message) {
				log.debug("Message from[" + from.getParticipant() + "]: " + message.getBody());
			}
		});
		chat.sendMessage("Hello!");
		
		chatManager.addChatListener(new ChatManagerListener() {
			public void chatCreated(Chat chat, boolean arg1) {
				log.debug("ChatParticipant: " + chat.getParticipant());
				chat.addMessageListener(new MessageListener() {
					public void processMessage(Chat chat, Message message) {
						log.debug("Message from[" + chat.getParticipant() + "]: " + message.getBody());
						
					}
				});
			}
		});
	}

	@Override
	protected void finalize() throws Throwable {
		this.connection.disconnect();
		super.finalize();
	}
}
